***********************************************
*** GENDER GAP ANALYSIS ***********************
*** ADDITIONAL PARTIES FOR BIVARIATE GRAPHS ***
*** QUINN M. ALBAUGH **************************
*** NOVEMBER 2023 VERSION *********************
***********************************************

*** SET WORKING DIRECTORY
clear all

*** CHANGE SETTINGS FOR GRAPHS
set more off, permanently
grstyle init
grstyle set plain, horizontal nogrid
graph set window fontface "Helvetica"

*** IMPORT DATA
use "${here}GenderGapAnalysis.dta", clear

*************************************
*** BIVARIATE RELATIONSHIP GRAPHS ***
*************************************

drop if genderid == 4

recode partyid (7=6)
recode partyid (8=7)
label define parties3 1 "Liberal" 2 "Conservative" 3 "NDP" 4 "BQ" 5 "Green" 6 "Other" 7 "None"
label values partyid parties3

tab partyid, gen(pid_)

recode pt (12=11) (13=11)

**********************
*** WEIGHTED, 2021 ***
**********************

**************************************
*** FIGURE 1: PARTY IDENTIFICATION ***
**************************************

*** LIBERAL
preserve
drop if genderid == .
drop if pid_1 == .

tab genderid pid_1 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN = sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = pid_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(30.58, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_LIB_W, replace
restore

*** CONSERVATIVE
preserve
drop if genderid == .
drop if pid_2 == .

tab genderid pid_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = pid_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(26.75, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_CON_W, replace
restore

*** NDP
preserve
drop if genderid == .
drop if pid_3 == .

tab genderid pid_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = pid_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(17.42, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_NDP_W, replace
restore

*** BQ
preserve
drop if genderid == .
drop if pid_4 == .

tab genderid pid_4 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen bq_w = pid_4 * cps21_weight_general_all
by genderid, sort: egen bq_w_sum = sum(bq_w)
by genderid, sort: gen p_hat = bq_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(7.17, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_BQ_W, replace
restore

*** GREEN
preserve
drop if genderid == .
drop if pid_5 == .

tab genderid pid_5 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen green_w = pid_5 * cps21_weight_general_all
by genderid, sort: egen green_w_sum = sum(green_w)
by genderid, sort: gen p_hat = green_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(3.09, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_GRN_W, replace
restore

*** NONE
preserve
drop if genderid == .
drop if pid_7 == .

tab genderid pid_7 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen green_w = pid_7 * cps21_weight_general_all
by genderid, sort: egen green_w_sum = sum(green_w)
by genderid, sort: gen p_hat = green_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("None", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(12.42, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_None_W, replace
restore

*** COMBINE PARTY IDENTIFICATION GRAPHS
graph combine PID_LIB_W.gph PID_CON_W.gph PID_NDP_W.gph PID_BQ_W.gph PID_GRN_W.gph PID_None_W.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Identifying with Party", size(small))
graph export "${here}GenderPID_Weighted_All.png", height(2400) width(2000) replace

******************************************
*** FIGURE 2: WEIGHTED VOTE INTENTIONS ***
******************************************

*** LIBERAL
preserve
drop if genderid == .
drop if vote_1 == .

tab genderid vote_1 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = vote_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(28.69, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_LIB_W, replace
restore

*** CONSERVATIVE
preserve
drop if genderid == .
drop if vote_2 == .

tab genderid vote_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = vote_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(31.17, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_CON_W, replace
restore

*** NDP
preserve
drop if genderid == .
drop if vote_3 == .

tab genderid vote_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = vote_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100, labsize(medsmall)) ///
	yline(24.42, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_NDP_W, replace
restore

*** BQ
preserve
drop if genderid == .
drop if vote_4 == .

tab genderid vote_4 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen bq_w = vote_4 * cps21_weight_general_all
by genderid, sort: egen bq_w_sum = sum(bq_w)
by genderid, sort: gen p_hat = bq_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(8.08, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_BQ_W, replace
restore

*** GREEN
preserve
drop if genderid == .
drop if vote_5 == .

tab genderid vote_5 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen green_w = vote_5 * cps21_weight_general_all
by genderid, sort: egen green_w_sum = sum(green_w)
by genderid, sort: gen p_hat = green_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(3.02, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_GRN_W, replace
restore

*** COMBINE VOTE INTENTION GRAPHS
graph combine Vote_LIB_W.gph Vote_CON_W.gph Vote_NDP_W.gph Vote_BQ_W.gph Vote_GRN_W.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Intending to Vote for Party", size(small))
graph export "GenderVoteIntention_Weighted_All.png", height(2400) width(2000) replace

******************************************************************
*** FIGURE 3: WEIGHTED PARTY IDENTIFICATION, LGBTQ ONLY SAMPLE ***
******************************************************************

tab partyid if lgbtq == 1 [aweight = cps21_weight_general_all]
tab genderid partyid if lgbtq == 1 [aweight = cps21_weight_general_all], row

*** LIBERAL
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_1 == .

tab genderid pid_1 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = pid_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(29.79, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_LIB_W_LGBTQ, replace
restore

*** CONSERVATIVE
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_2 == .

tab genderid pid_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = pid_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(12.84, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_CON_W_LGBTQ, replace
restore

*** NDP
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_3 == .

tab genderid pid_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = pid_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(30.67, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_NDP_W_LGBTQ, replace
restore

*** BQ
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_4 == .

tab genderid pid_4 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = pid_4 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(6.35, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_BQ_W_LGBTQ, replace
restore

*** GREEN
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_5 == .

tab genderid pid_5 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = pid_5 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(4.83, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_GRN_W_LGBTQ, replace
restore

*** NONE
preserve
keep if lgbtq == 1
drop if genderid == .
drop if pid_7 == .

tab genderid pid_7 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = pid_7 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(12.92, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save PID_None_W_LGBTQ, replace
restore

*** COMBINE PARTY IDENTIFICATION GRAPHS
graph combine PID_LIB_W_LGBTQ.gph PID_CON_W_LGBTQ.gph PID_NDP_W_LGBTQ.gph PID_BQ_W_LGBTQ.gph PID_GRN_W_LGBTQ.gph PID_None_W_LGBTQ.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Identifying with Party", size(small))
graph export "${here}GenderPID_Weighted_LGBTQ_All.png", height(2400) width(2000) replace
*/

************************************************************
*** FIGURE 4: WEIGHTED VOTE INTENTION, LGBTQ ONLY SAMPLE ***
************************************************************

tab voteintention if lgbtq == 1 [aweight = cps21_weight_general_all]
tab genderid voteintention if lgbtq == 1 [aweight = cps21_weight_general_all], row

*** LIBERAL
preserve
keep if lgbtq == 1
drop if genderid == .
drop if vote_1 == .

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen liberal_w = vote_1 * cps21_weight_general_all
by genderid, sort: egen liberal_w_sum = sum(liberal_w)
by genderid, sort: gen p_hat = liberal_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Liberal", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(26.87, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_LIB_W_LGBTQ, replace
restore

*** CONSERVATIVE
preserve
keep if lgbtq == 1
drop if genderid == .
drop if vote_2 == .

tab genderid vote_2 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen conservative_w = vote_2 * cps21_weight_general_all
by genderid, sort: egen conservative_w_sum = sum(conservative_w)
by genderid, sort: gen p_hat = conservative_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Conservative", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(16.49, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_CON_W_LGBTQ, replace
restore

*** NDP
preserve
keep if lgbtq == 1
drop if genderid == .
drop if vote_3 == .

tab genderid vote_3 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen ndp_w = vote_3 * cps21_weight_general_all
by genderid, sort: egen ndp_w_sum = sum(ndp_w)
by genderid, sort: gen p_hat = ndp_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("NDP", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100, labsize(medsmall)) ///
	yline(39.87, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_NDP_W_LGBTQ, replace
restore

*** BQ
preserve
drop if genderid == .
drop if vote_4 == .

tab genderid vote_4 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen bq_w = vote_4 * cps21_weight_general_all
by genderid, sort: egen bq_w_sum = sum(bq_w)
by genderid, sort: gen p_hat = bq_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("BQ", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(6.91, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_BQ_W_LGBTQ, replace
restore

*** GREEN
preserve
drop if genderid == .
drop if vote_5 == .

tab genderid vote_5 [aweight=cps21_weight_general_all], row

by genderid, sort: egen wN= sum(cps21_weight_general_all)
by genderid, sort: gen green_w = vote_5 * cps21_weight_general_all
by genderid, sort: egen green_w_sum = sum(green_w)
by genderid, sort: gen p_hat = green_w_sum / wN
by genderid, sort: gen middle90 = (p_hat + (1.64^2/(2*wN))) / (1 + ((1.64^2)/(wN)))
by genderid, sort: gen lower90 = middle90 - (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen upper90 = middle90 + (1.64*sqrt((p_hat*(1-p_hat)) + ((1.64^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.64^2)/wN))))
by genderid, sort: gen middle95 = (p_hat + (1.96^2/(2*wN))) / (1 + ((1.96^2)/(wN)))
by genderid, sort: gen lower95 = middle95 - (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
by genderid, sort: gen upper95 = middle95 + (1.96*sqrt((p_hat*(1-p_hat)) + ((1.96^2)/(4*wN))))/((sqrt(wN)*(1 + ((1.96^2)/wN))))
collapse p_hat lower90 upper90 lower95 upper95, by(genderid)
replace p_hat = p_hat * 100
replace lower90 = lower90 * 100
replace upper90 = upper90 * 100
replace lower95 = lower95 * 100
replace upper95 = upper95 * 100

graph twoway ///
	(scatter p_hat genderid, mcolor(black) msymbol(Oh) mlabel(p_hat)  mlabposition(3) mlabcolor(black) mlabformat(%9.1f)) ///
	(rspike lower90 upper90 genderid, lcolor(black) lwidth(medthick)) ///
	(rspike lower95 upper95 genderid, lcolor(black) lwidth(thin)), ///
	title("Green", size(medsmall)) ///
	xscale(range(0.75 3.25) titlegap(2)) ///
	xtitle("") ///
	xlabel(1 "Man" 2 "Woman" 3 "Nonbinary", labsize(medsmall)) ///
	yscale(range(0 100) titlegap(2)) ///
	ylabel(0(25)100) ///
	yline(4.51, lcolor(gs14) lpattern(solid)) ///
	legend(off)
graph save Vote_GRN_W_LGBTQ, replace
restore

*** COMBINE VOTE INTENTION GRAPHS
graph combine Vote_LIB_W_LGBTQ.gph Vote_CON_W_LGBTQ.gph Vote_NDP_W_LGBTQ.gph Vote_BQ_W_LGBTQ.gph Vote_GRN_W_LGBTQ.gph, col(2) ycommon xcommon b1("Gender Identity", size(small)) l1("Percent Intending to Vote for Party", size(small))
graph export "GenderVoteIntention_Weighted_LGBTQ_All.png", height(2400) width(2000) replace
